from PIL import Image, ImageDraw, ImageFont
import textwrap
from moviepy.editor import ImageClip
import numpy as np


def get_font_render_size(text, font_path, font_size):
    """
    :param text: 文字
    :param font_path: 字体路径
    :param font_size: 字体大小
    :return: tuple 宽高
    """
    canvas = Image.new('RGB', (2048, 2048))
    ImageDraw.Draw(canvas).text((0, 0),
                                text, font=ImageFont.truetype(font_path, font_size))
    bbox = canvas.getbbox()
    return bbox[2] - bbox[0], bbox[3] - bbox[1]


def main(text, templ_parm,font_ttf_session, space_length=20, max_line_length=14, line_spacing=15):
    # 将文本分割成多行
    lines = textwrap.wrap(text, width=max_line_length)

    # 计算文字渲染后的长度
    max_text_width = 0
    total_text_height = 0
    for line in lines:
        text_width, text_height = get_font_render_size(line, font_ttf_session, templ_parm['bubble']['fontSize'])
        max_text_width = max(max_text_width, text_width)
        total_text_height += text_height + line_spacing  # 增加行间距

    # 减去最后一行的额外行间距
    total_text_height -= line_spacing
    # 定义背景宽度
    bg_width = max_text_width + space_length * 2  # 增加左右边距
    bg_height = total_text_height + space_length * 3  # 增加上下边距
    bg_height = max(bg_height, templ_parm['head']['wh'])
    # 生成背景
    bg_img = Image.new(mode='RGBA', size=(bg_width + 50, bg_height), color=(255, 255, 255, 0))

    # 读取头像文件，并绘制头像到背景图片

    # 绘制气泡
    ## 1. 绘制圆角矩形
    x, y, w, h, r = space_length, 0, max_text_width + space_length * 1.5 + 15, bg_height, space_length
    fill_color = "#FFFFFF"
    draw_object = ImageDraw.Draw(bg_img)

    draw_object.ellipse((x, y, x + r, y + r), fill=fill_color)
    draw_object.ellipse((x + w - r, y, x + w, y + r), fill=fill_color)
    draw_object.ellipse((x, y + h - r, x + r, y + h), fill=fill_color)
    draw_object.ellipse((x + w - r, y + h - r, x + w, y + h), fill=fill_color)

    draw_object.rectangle((x + r / 2, y, x + w - (r / 2), y + h), fill=fill_color)
    draw_object.rectangle((x, y + r / 2, x + w, y + h - (r / 2)), fill=fill_color)

    ## 2. 绘制三角形 （对话的哪个尖尖） 26.0  16.0  36.0

    if len(lines) > 1:
        # 多行文本的尖尖坐标
        draw_object.polygon([
            (space_length / 2, 60.0),
            (space_length, 50.0),
            (space_length, 70.0),
        ], fill="#FFFFFF")
    else:
        # 单行文本的尖尖坐标
        draw_object.polygon([
            (space_length / 2, bg_height / 2),
            (space_length, bg_height / 2 - space_length / 2),
            (space_length, bg_height / 2 + space_length / 2),
        ], fill="#FFFFFF")
    ## 3. 绘制字体
    y_text = space_length  # 从顶部边距开始绘制文本
    for line in lines:
        draw_object.text((space_length + 15, y_text),
                         line, fill='#000000', font=ImageFont.truetype(font_ttf_session, templ_parm['bubble']['fontSize']))
        _, text_height = get_font_render_size(line, font_ttf_session, templ_parm['bubble']['fontSize'])
        y_text += text_height + line_spacing  # 使用行间距

    img_clip = ImageClip(np.array(bg_img), duration=1)
    w, h = img_clip.size
    return img_clip, w, h


if __name__ == "__main__":
    main("如果你哇哇哇哇哇哇哇哇哇")  # 更改此处文字即可。
